package com.peanut.controller;


import com.alibaba.fastjson.JSONObject;
import com.peanut.common.Result;
import com.peanut.entity.BO.TabItemBO;
import com.peanut.entity.PO.TabItemPO;
import com.peanut.entity.PO.TabOrderItemPO;
import com.peanut.entity.VO.TabItemVO;
import com.peanut.mapper.TabItemMapper;
import com.peanut.mapper.TabOrderItemMapper;
import com.peanut.service.ITabItemService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("recommendController")
public class RecommendController {

    @Autowired
    private ITabItemService iTabItemService;
    @Autowired
    private TabItemMapper tabItemMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TabOrderItemMapper tabOrderItemMapper;

    @PostMapping("/selectOrder")
    @ApiOperation("根据车况进行排序查询")
    public Result test(){
        List<TabItemPO> list = tabItemMapper.selectInfo();
        return Result.OK().setData(list);
    }

    @PostMapping("/selectHot")
    @ApiOperation("查询车辆热度")
    public Result selectRedis(){
        try {
            Set<String> zSet = redisTemplate.boundZSetOps("peanut_hot").reverseRange(0, 9);
            //判断redis中是否有数据
            if (zSet.size() == 0) {
                //没有就进入数据库查询数据
                List<TabItemPO> tabItemBOList = tabItemMapper.selectList(null);
                List<TabItemBO> tabItemDTOList = new ArrayList<>();
                //转为CarDTO
                for (TabItemPO tabItemVO : tabItemBOList) {
                    TabItemBO itemDTO = new TabItemBO();
                    BeanUtils.copyProperties(tabItemVO, itemDTO);
                    tabItemDTOList.add(itemDTO);
                }
                for (TabItemBO itemDTO : tabItemDTOList) {
                    String itemDTOJson = JSONObject.toJSONString(itemDTO);
                    redisTemplate.boundZSetOps("peanut_hot").add(itemDTOJson, 0);
                }
                zSet = redisTemplate.boundZSetOps("peanut_hot").reverseRange(0, 9);
            }
            //定义一个集合接收缓存数据
            List<TabItemVO> itemVOList = new ArrayList<>();
            for (String s : zSet) {
                TabItemBO itemDTO = JSONObject.parseObject(s, TabItemBO.class);
                TabItemVO itemVO = new TabItemVO();
                BeanUtils.copyProperties(itemDTO, itemVO);
                itemVOList.add(itemVO);
            }
            return Result.OK().setData(itemVOList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @PostMapping("/selectMon")
    @ApiOperation("查询车辆热度")
    public Result selectMon(){
        List<TabOrderItemPO> list = tabOrderItemMapper.selectMoney();
        return Result.OK().setData(list);
    }
}
